<?php

namespace ApplicationTest\Modelservicetest;

use Application\Model\PacienteTable;
use Application\Model\Paciente;
use Zend\Db\ResultSet\ResultSet;
use PHPUnit_Framework_TestCase;

class PacienteTableTest extends PHPUnit_Framework_TestCase {

    public function testFetchAllReturnsAllPacientes() {
        $resultSet = new ResultSet();
        $mockTableGateway = $this->getMock('Zend\Db\TableGateway\TableGateway', array('select'), array(), '', false);
        $mockTableGateway->expects($this->once())
                ->method('select')
                ->with()
                ->will($this->returnValue($resultSet));

        $pacienteTable = new PacienteTable($mockTableGateway);

        $this->assertSame($resultSet, $pacienteTable->fetchAll());
    }

    public function testCanRetrieveAnAlbumByItsId() {
        $paciente = new Paciente();
        $paciente->exchangeArray(array('id' => 123,
            'artist' => 'The Military Wives',
            'title' => 'In My Dreams'));

        $resultSet = new ResultSet();
        $resultSet->setArrayObjectPrototype(new Paciente());
        $resultSet->initialize(array($paciente));

        $mockTableGateway = $this->getMock('Zend\Db\TableGateway\TableGateway', array('select'), array(), '', false);
        $mockTableGateway->expects($this->once())
                ->method('select')
                ->with(array('id' => 123))
                ->will($this->returnValue($resultSet));

        $pacienteTable = new PacienteTable($mockTableGateway);

        $this->assertSame($paciente, $pacienteTable->getPaciente(123));
    }

    public function testCanDeleteAnPacienteByItsId() {
        $mockTableGateway = $this->getMock('Zend\Db\TableGateway\TableGateway', array('delete'), array(), '', false);
        $mockTableGateway->expects($this->once())
                ->method('delete')
                ->with(array('id' => 123));

        $pacienteTable = new PacienteTable($mockTableGateway);
        $pacienteTable -> deletePaciente(123);
    }

    public function testSavePacienteWillInsertNewPacientesIfTheyDontAlreadyHaveAnId() {
        $pacienteData = array('artist' => 'The Military Wives', 'title' => 'In My Dreams');
        $paciente = new Paciente();
        $paciente->exchangeArray($pacienteData);

        $mockTableGateway = $this->getMock('Zend\Db\TableGateway\TableGateway', array('insert'), array(), '', false);
        $mockTableGateway->expects($this->once())
                ->method('insert')
                ->with($pacienteData);

        $pacienteTable = new PacienteTable($mockTableGateway);
        $pacienteTable->savePaciente($paciente);
    }

    public function testSavePacienteWillUpdateExistingPacientesIfTheyAlreadyHaveAnId() {
        $pacienteData = array('id' => 123, 'artist' => 'The Military Wives', 'title' => 'In My Dreams');
        $paciente = new Paciente();
        $paciente->exchangeArray($pacienteData);

        $resultSet = new ResultSet();
        $resultSet->setArrayObjectPrototype(new Paciente());
        $resultSet->initialize(array($paciente));

        $mockTableGateway = $this->getMock('Zend\Db\TableGateway\TableGateway', array('select', 'update'), array(), '', false);
        $mockTableGateway->expects($this->once())
                ->method('select')
                ->with(array('id' => 123))
                ->will($this->returnValue($resultSet));
        $mockTableGateway->expects($this->once())
                ->method('update')
                ->with(array('artist' => 'The Military Wives', 'title' => 'In My Dreams'), array('id' => 123));

        $pacienteTable = new PacienteTable($mockTableGateway);
        $pacienteTable->savePaciente($paciente);
    }

    public function testExceptionIsThrownWhenGettingNonexistentPaciente() {
        $resultSet = new ResultSet();
        $resultSet->setArrayObjectPrototype(new Paciente());
        $resultSet->initialize(array());

        $mockTableGateway = $this->getMock('Zend\Db\TableGateway\TableGateway', array('select'), array(), '', false);
        $mockTableGateway->expects($this->once())
                ->method('select')
                ->with(array('id' => 123))
                ->will($this->returnValue($resultSet));

        $pacienteTable = new PacienteTable($mockTableGateway);

        try {
            $pacienteTable->getPaciente(123);
        } catch (\Exception $e) {
            $this->assertSame('Could not find row 123', $e->getMessage());
            return;
        }

        $this->fail('Expected exception was not thrown');
    }

}